<%@page import="com.jungo.web.bean.User"%>
<%@page import="com.jungo.web.bean.Product"%>
<%@page import="com.jungo.web.bean.Favorite"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<%@	taglib prefix="c"			uri="http://java.sun.com/jsp/jstl/core"%>
<%@	taglib prefix="fmt"		uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@	taglib prefix="fn"		uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="spring"	uri="http://www.springframework.org/tags" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).bind("mobileinit", function () {
    $.mobile.ajaxEnabled = false;
});
</script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<title>중고마트</title>
</head>
<body>
<%
	String fromPage = request.getParameter("fromPage");

	boolean visibleDealButton = false;

	Product product = (Product)request.getAttribute("product");
	if ( request.getSession().getAttribute("user") != null ) {
		User user = (User)request.getSession().getAttribute("user");
		if ( product.getUserid().equals(user.getUserid()) == false ) {
			visibleDealButton = true;
		}
	}
	
	Boolean dealButton = (Boolean)request.getAttribute("dealButton");
	if ( dealButton != null && dealButton == false ) {
		visibleDealButton = false;
	}
	
	Favorite favorite = null;
	if ( request.getAttribute("favorite") != null ) {
		favorite = (Favorite)request.getAttribute("favorite");
	}
	
%>
<section id="product" data-role="page">
    <header data-role="header" data-position="fixed">
		<a href="index.do" data-role="button" data-icon="home" data-iconpos="notext" data-inline="true">Home</a>
		<h1 style="text-overflow:clip;margin-left:10%;margin-right:10%">상품 상세</h1>
		<a id="logout" data-role="button" href="logout.do" class="ui-btn-right" data-ajax="false">로그아웃</a>
	</header>
    <div data-role="content">
    	<div class="productImage">
    		<img alt="상품이미지" src="${ product.image_path }" width="100%" height="100%" style="margin: 0pt auto; display: table-cell; text-align: center; vertical-align: middle;">
    	</div>
        <h2>${ product.title }</h2>
        <h3>상품가격: ${ product.price }원</h3>
        <h3>등록자: ${ product.userid }</h3>
        <p>${ product.description }</p>
    </div>
    
    <c:set var="fromPage" value="<%= fromPage %>"/>
    <c:set var="visibleDealButton" value="<%= visibleDealButton %>"/>
    <c:set var="favorite" value="<%= favorite %>"/>
    
    <c:if test="${ favorite != null }">
    	<hr/>
    	<form id="productForm" name="productForm" action="product.do" method="post" data-ajax="false">
    	<div class="favorite">
    		<div class="favorite-inner">
    		<p>${ favorite.userid }님이 아래와 같은 거래 신청을 하셨습니다.</p>
    		<p>키워드: ${ favorite.title }<br>
    			최소가격: ${ favorite.min_price }원<br>
    			최고가격: ${ favorite.max_price }원<br>
    			연락처: ${ favorite.contact }
	        </p>
	        <fieldset class="ui-grid-a">
    		<div class="ui-block-a"><button id="complete" name="complete" type="submit" data-theme="c">거래완료</button></div>
    		<div class="ui-block-b"><button id="cancel" name="cancel" type="submit" data-theme="c">거래취소</button></div>
			</fieldset>
			<input type="hidden" name="pid" id="pid" value="${ product.pid }">
			<input type="hidden" name="submitValue" id="submitValue">
			<input type="hidden" name="fid" id="fid" value="${favorite.fid }">
			<input type="hidden" name="fromPage" value="${ fromPage }">
			</div>
    	</div>
    	</form>
    </c:if>
    
    <div id="dealWithDiv" data-role="content">
        <a id="dealWith" href="deal.do?pid=${ product.pid }" data-role="button" data-ajax="false">거래신청(관심상품등록)</a>
    </div>

    <footer data-role="footer" data-position="fixed">
    	<div data-role="navbar" data-iconpos="left">
			<ul>
				<li><a id="products" href="products.do" data-icon="grid" rel="external">전체 상품</a></li>
				<li><a id="myProducts" href="myProducts.do" data-icon="bars" rel="external">내 상품</a></li>
				<li><a id="myFavorites" href="myFavorites.do" data-icon="star" rel="external">내 관심상품</a></li>
			</ul>
		</div><!-- /navbar -->
    </footer>
</section>
</body>
<style>
.flexslider { margin: 0px 15px; }
.category-content { margin: 40px 15px; }
.productImage { border:1px solid silver; height:200px; }
.favorite { border:2px solid silver; margin:15px; }
.favorite-inner { margin:5px; }
hr { border-top:1px solid #9C9C9C; border-bottom:1px solid #F6F6F6; margin: 10px;}
</style>
<script>
$(document).bind('pageinit', function() {
	
	if ( "${ fromPage }" == "productList" ) {
		$('#products').addClass("ui-btn-active");
	} else if ( "${ fromPage }" == "myProducts" ) {
		$('#myProducts').addClass("ui-btn-active");
	}
	
	$('#logout').hide();
	var user = "${sessionScope.user.userid}";
	
	if ( user != null && "" != user ) {
		$('#logout').show();
	}
	
	$('#dealWith').hide();
	
	if ( user != null && "" != user ) {
		if ( "${ visibleDealButton }" == 'false' ) {
			$('#dealWith').hide();
		} else {
			$('#dealWith').show();
		}
	} else {
		$('#dealWith span span').text("거래신청(로그인필요)");
		$('#dealWith').show();
	}
	
	$('#productForm').submit(function() {
		
		if ( $('#submitValue').val() == "" ) {
			alert("undefined submitValue");
		}
		return true;
	});
	
	$('#complete').bind('click', function() {
		$('#submitValue').val("complete");
	});
	
	$('#cancel').bind('click', function() {
		$('#submitValue').val("cancel");
	});
});
</script>
</html>